// pages/homeRegion/homeRegion.js
// 地区城市页
// var city = require('../../libs/city.js')
var lineHeight = 0;
var endWords = "";
var isNum;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    "hidden": true,
    cityName: "暂无", //获取选中的城市名
    cityId: null,
    cityData: []
  },
  sureBtn: function() {
    // 地区确定
    wx.redirectTo({
      url: '/pages/homeScreen/homeScreen?cityId=' + this.data.cityId + '&cityName=' + this.data.cityName,
    })
  },
  //触发全部开始选择
  chStart: function() {
    this.setData({
      trans: ".3",
      hidden: false
    })
  },
  //触发结束选择
  chEnd: function() {
    this.setData({
      trans: "0",
      hidden: true,
      scrollTopId: this.endWords
    })
  },
  //获取文字信息
  getWords: function(e) {
    var id = e.target.id;
    this.endWords = id;
    isNum = id;
    this.setData({
      showwords: this.endWords
    })
  },
  //设置文字信息
  setWords: function(e) {
    var id = e.target.id;
    this.setData({
      scrollTopId: id
    })
  },

  // 滑动选择城市
  chMove: function(e) {
    var y = e.touches[0].clientY;
    var offsettop = e.currentTarget.offsetTop;
    var height = 0;
    var that = this;;
    var cityarr = ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z"]
    // 获取y轴最大值
    wx.getSystemInfo({
      success: function(res) {
        height = res.windowHeight - 10;
      }
    });

    //判断选择区域,只有在选择区才会生效
    if (y > offsettop && y < height) {
      // console.log((y-offsettop)/lineHeight)
      var num = parseInt((y - offsettop) / lineHeight);
      endWords = cityarr[num];
      // 这里 把endWords 绑定到this 上，是为了手指离开事件获取值
      that.endWords = endWords;
    };


    //去除重复，为了防止每次移动都赋值 ,这里限制值有变化后才会有赋值操作，
    //DOTO 这里暂时还有问题，还是比较卡，待优化
    if (isNum != num) {
      // console.log(isNum);
      isNum = num;
      that.setData({
        showwords: that.endWords
      })
    }
  },
  //选择城市，并让选中的值显示在文本框里
  bindCity: function(e) {
    let cityId = e.currentTarget.dataset.cityid;
    let cityName = e.currentTarget.dataset.cityname
    this.setData({
      cityId: cityId,
      cityName: cityName
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    let config = wx.getStorageSync('config')
    wx.request({
      url: config.host + 'api/city/city',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        if (res.errMsg == 'request:ok') {
          let list = res.data.data
          let city = {}
          list.forEach(item => {
            city[item.initial] = item.child
          })
          that.setData({
            cityData: city
          })
        } else {
          wx.showToast({
            title: '城市数据获取失败',
            icon: 'none'
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    // 生命周期函数--监听页面初次渲染完成
    var cityChild = this.data.cityData;
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        lineHeight = (res.windowHeight - 100) / 22;
        // console.log(res.windowHeight - 100)
        that.setData({
          city: cityChild,
          winHeight: res.windowHeight - 40,
          // winHeight: res.windowHeight - 120,
          lineHeight: lineHeight
        })
      }
    })
  }
})